package com.joshua.spring.gateway.config;

import com.joshua.spring.gateway.constant.SwaggerConstants;
import com.joshua.spring.gateway.entity.bo.RouteDefinitionGroup;
import com.joshua.spring.gateway.repo.RedisRouteDefinitionRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.*;

import static io.netty.handler.codec.http.cookie.CookieHeaderNames.PATH;
import static io.swagger.models.properties.PropertyBuilder.PropertyId.PATTERN;


/**
 * description
 * package com.joshua.spring.gateway.config
 * classname  SwaggerResourceConfig
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/6/6 16:21
 */
@Slf4j
@Component
@Primary
@AllArgsConstructor
public class SwaggerResourceConfig implements SwaggerResourcesProvider {

    @Autowired
    private RedisRouteDefinitionRepository redisRouteDefinitionRepository;
    private RouteLocator routeLocator;

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> routes = new ArrayList<>();
        List<RouteDefinition> list = new ArrayList<>();
        routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
        if (!CollectionUtils.isEmpty(redisRouteDefinitionRepository.ROUTE_DEFINITION_GROUP)) {
            redisRouteDefinitionRepository.ROUTE_DEFINITION_GROUP.values().forEach(e -> {
                list.add(e.getRouteDefinition());
            });
            list.stream().filter(routeDefinition ->
                    routes.contains(routeDefinition.getId())).forEach(route -> route.getPredicates().stream()
                    .filter(predicateDefinition -> (PATH).equalsIgnoreCase(predicateDefinition.getName()))
                    .forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(),
                            predicateDefinition.getArgs().get(PATTERN.getPropertyName())
                                    .replace(SwaggerConstants.ALL_MATCH, SwaggerConstants.DOCS_URI_WITH_PREFIX)))));
        }
        return resources;
    }

    private SwaggerResource swaggerResource(String routeId, String location) {
        String groupName = routeId;
        RouteDefinitionGroup routeDefinitionGroup = redisRouteDefinitionRepository.ROUTE_DEFINITION_GROUP.get(routeId);
        groupName = StringUtils.isEmpty(routeDefinitionGroup.getApplicationName())
                ? routeDefinitionGroup.getApplicationCode() : routeDefinitionGroup.getApplicationName();
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(groupName);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(SwaggerConstants.V2);
        log.info("Swagger bootstrap ui name:{},location:{}", groupName, location);
        return swaggerResource;
    }
}
